package com.zd.kids.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.hyy.libray.common.BaseUtils;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.R;
import com.zd.kids.common.Constants;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;

public class InputLoginMobileActivity extends BaseActivity {

    @BindView(R.id.ed_input_mobile)
    EditText edInputMobile;
    @BindView(R.id.tv_area)
    TextView tvArea;

    /**
     * 是密码失效，跳转到登录页
     */
    private boolean isPswError;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_input_login_mobile;
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        initTitle(getString(R.string.login_qm));

        isPswError = getIntent().getBooleanExtra("PswError", false);
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }


    @OnClick({R.id.tv_area, R.id.tv_confirm})
    public void onViewClicked(View view) {
        if (BaseUtils.isFastClick()) {
            return;
        }
        KeyboardUtils.hideSoftInput(this);
        Intent intent;
        switch (view.getId()) {
            case R.id.tv_area:
                intent = new Intent(this, SelectCityActivity.class);
                startActivityForResult(intent, SelectCityActivity.SELECT_RESULT_CODE);
                break;
            case R.id.tv_confirm:
                String mobile = edInputMobile.getText().toString();
                getCode(mobile, cityId);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (SelectCityActivity.SELECT_RESULT_CODE == requestCode && resultCode == SelectCityActivity.SELECT_RESULT_CODE) {
            String cityCode = data.getStringExtra("cityCode");
            cityId = data.getStringExtra("cityId");
            tvArea.setText(cityCode);
        }
    }

    private String cityId = Constants.HONG_KONG_DEFAULT_CITY_ID;

    private void getCode(String mobile, String cityId) {
        showLoading();
        HttpMethod.getIntence().getloginMobileSms(mobile, cityId, new CustomObserver<String>() {
            @Override
            public void onSuccess(String code) {
                disLoading();
                isPswError = false;
                Intent intent = new Intent(InputLoginMobileActivity.this, InputCodeActivity.class);
                intent.putExtra(InputCodeActivity.MOBILE_KEY, mobile);
                intent.putExtra("Login", true);
                intent.putExtra(InputCodeActivity.MOBILE_COUNTRY_KEY, cityId);
                intent.putExtra("code", code);
                startActivity(intent);
                finish();
            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }

    @Override
    public void finish() {

        if (isPswError) {
            Intent intent = new Intent(this, WelcomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }

        super.finish();
    }
}
